﻿@using Coralcode.Adapter.TextualAnalyzer.Entity.Domains
@using Coralcode.Base.Entity
@using Coralcode.Base.Utils
@using CoralNode.Core.Entity.Domains.User
@using CoralNode.Core.Entity.Enums
@using CoralNode.MvcClient.Helper
@using CoralNode.Core.Entity.Domains.Prototype
@using CoralNode.MvcClient.Contexts
@model SearchIndexDomain
@{
    var searchIndexs = ViewBag.SearchIndexs as IList<SearchIndexDomain>;
    searchIndexs = searchIndexs ?? new List<SearchIndexDomain>();
    IList<PrototypeDomain> prototypes = ViewBag.Prototypes;
    Layout = null; 
}
<style>
    .checkbox_own { margin-top: 6px; }
    .jurisdiction_cnt { border: 1px solid #a9afb4; width: 315px; }
    .jurisdiction_cnt .cnt { min-width: 200px; height: auto; overflow: hidden; }
    .list_jurisdictionlist ul { height: auto; }
</style>
<form id="spform" action="/m/sitesetting/SaveSiteIndexSetting" method="post">
    <div class="table_form form_main ">
        <table>
            <tr>
                <th width="60">标题：
                </th>
                <td>
                    @Html.TextBoxFor(n => n.Title, new { @class = "text w_37", data_meta = "{required:true}" })
                </td>
            </tr>
            <tr>
                <th>编码：</th>
                <td>
                    @Html.TextBoxFor(n => n.Key, new { @class = "text w_37", data_meta = "{required:true}" })
                </td>
            </tr>
            @if (prototypes != null && prototypes.Count > 0) {
                <tr>
                    <th>字段：</th>
                    <td>
                        <div class="jurisdiction_cnt">
                            <div class="cnt">
                                <div class="list_jurisdictionlist">
                                    <div class="clearfix list_cnt">
                                        <ul>
                                            @foreach (var item in prototypes) {
                                                <li class="more">
                                                    @if (Model.Fields.Select(n => n.FieldName).Any(n => n.StartsWith(item.Code, StringComparison.CurrentCultureIgnoreCase))) {
                                                        <label class="checkbox_own selected">
                                                            <input type="checkbox" checked="checked" /></label>@item.Title
                                                    } else {
                                                        <label class="checkbox_own">
                                                            <input type="checkbox" /></label>@item.Title    
                                                    }

                                                    <ul class="d_n" style="border-right: 0;">
                                                        @foreach (var field in item.FieldList) {
                                                            <li>
                                                                <label class="checkbox_own @(Model.Fields.Select(n => n.FieldName).Contains(string.Format("{0}.{1}", item.Code, field.FieldName)) ? "selected" : string.Empty)">
                                                                    <input type="checkbox" @(Model.Fields.Select(n => n.FieldName).Contains(string.Format("{0}.{1}", item.Code, field.FieldName)) ? "checked=checked" : string.Empty)  name="fields" value="@string.Format("{0}.{1}", item.Code, field.FieldName)"/>
                                                                </label>@field.Title
                                                            </li>
                                                        }
                                                    </ul>
                                                </li>
                                            }
                                        </ul>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </td>
                </tr>    
            }
        </table>
    </div>
    @Html.HiddenFor(n => n.Id)
</form>
<script>
    $(function () {

        function removeSelected(node) {
            node.removeClass("selected");
            node.find("input[type='checkbox']").removeAttr("checked");
        }

        function addSelected(node) {
            node.addClass("selected");
            node.find("input[type='checkbox']").attr("checked", "checked");
        }
        //权限
        $(".list_jurisdictionlist ul li").click(function (e) {
            e.stopPropagation();
            var $this = $(this);
            $this.addClass("current").siblings("li").removeClass("current");
            if ($this.children("ul").length > 0) {
                $this.children("ul").children("li:eq(0)").trigger("click");
            }
        });

        //阻止.checkbox_own触发两次
        $(".list_jurisdictionlist ul li .checkbox_own input").click(function (e) {
            $(this).parent().trigger("click");
        });

        $(".list_jurisdictionlist ul li .checkbox_own").click(function (e) {
            var $this = $(this);
            $this.trigger("click.own");
            if ($this.hasClass("selected")) {
                $this.parent().find("ul .checkbox_own").each(function () {
                    addSelected($(this));
                });
                addSelected($this.parent().parent().siblings(".checkbox_own"));
            } else {
                $this.parent().find("ul .checkbox_own").each(function () {
                    removeSelected($(this));
                });
                if ($this.parent().siblings().children(".checkbox_own.selected").length == 0) {
                    removeSelected($this.parent().parent().siblings(".checkbox_own"));
                }
            }
        });
        $(".list_jurisdictionlist > div > ul >li:first").trigger("click");
    })
</script>
